#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using GameStateManagement;
#endregion


// ****************************************************************************** //
//   This component handles the rendering of all the 2d elements in the game      //
//             including GUI & background, but also waves animation               //
// ****************************************************************************** //

namespace Frogger_Game
{

    class Rendering : Microsoft.Xna.Framework.DrawableGameComponent
    {
        #region Fields

        //textures
        SpriteBatch spriteBatch;
        Texture2D back;
        Texture2D wave;
        Texture2D empty;
        Vector2 wave_pos;
        Vector2 wave2_pos;
        float scale;


        //fonts
        SpriteFont spriteFont;
        SpriteFont startFont;
        SpriteFont splashFont; //previous font: <FontName>Frankfurt</FontName>

        //game timer
        float timer;
        GameTimer t;

        #endregion

        #region Initialization

        /// <summary>
        /// Constructor.
        /// </summary>
        public Rendering(Game game)
            : base(game)
        {
            scale = 1.0f;
        }

        /// <summary>
        /// Initialize all non-graphics content
        /// </summary>
        public override void Initialize()
        {
            timer = 0.0f;
            t = (GameTimer)Game.Services.GetService(typeof(GameTimer));
            base.Initialize();
        }

        /// <summary>
        /// Load all graphics content
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            back = Game.Content.Load<Texture2D>("game_background");
            empty = Game.Content.Load<Texture2D>("blank");
            spriteFont = Game.Content.Load<SpriteFont>("gamefont");
            splashFont = Game.Content.Load<SpriteFont>("splashfont");
            startFont = Game.Content.Load<SpriteFont>("startfont");
            wave = Game.Content.Load<Texture2D>("waves");

            //set starting wave position
            wave_pos = new Vector2(0.0f, 0.0f);
            wave2_pos = new Vector2(wave_pos.X + wave.Width, 0.0f);

            base.LoadContent();
        }


        protected override void Dispose(bool disposing)
        {

            base.Dispose(disposing);
        }


        #region Update and Draw
        /// <summary>
        /// Update method
        /// </summary>
        /// <param name="gameTime">Time of the game</param>
        public override void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            //updates waves positions
            if (wave_pos.X < -wave.Width)
            {

                wave_pos.X = wave2_pos.X + wave.Width;

            }

            if (wave2_pos.X < -wave.Width)
            {

                wave2_pos.X = wave_pos.X + wave.Width;

            }

            //set waves animation speed
            wave_pos.X += -elapsed * 150;
            wave2_pos.X += -elapsed * 150;

            base.Update(gameTime);
        }


       /// <summary>
        /// Draws the 2d graphics on the screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
           GraphicsDevice.Clear(Color.Black);

           var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

           float scalew = (float)GraphicsDevice.Viewport.Width / (float)back.Width;
           float scaleh = (float)GraphicsDevice.Viewport.Height / (float)back.Height;
           Vector2 scalefactor = new Vector2(scalew,scaleh);

            string life = string.Format("Life: " + GameState.game_state.Life);

            //start drawing...
            spriteBatch.Begin();
       
            //... background
            spriteBatch.Draw(back, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), 
                new Rectangle(0, 0, back.Width, back.Height), Color.White,0.0f,Vector2.Zero,SpriteEffects.None,0);
            
            //... waves
            spriteBatch.Draw(wave, wave_pos * scalefactor, null, Color.White,
                      0.0f, Vector2.Zero, scalefactor, SpriteEffects.None, 1);
            spriteBatch.Draw(wave, wave2_pos * scalefactor, null, Color.White,
                        0.0f, Vector2.Zero, scalefactor, SpriteEffects.None, 1);

            float width_ratio = (float)GraphicsDevice.Viewport.Width / 1366.0f;
            float height_ratio = (float)GraphicsDevice.Viewport.Height / 768.0f; 

            //start drawing text (GUI)
            //we change text size accordingly to screen resolution
            if (GraphicsDevice.Viewport.Width > 1366)
            {
                spriteBatch.DrawString(spriteFont, life, new Vector2(82, 690) * new Vector2(width_ratio,height_ratio),
                    Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
                spriteBatch.DrawString(spriteFont, life, new Vector2(81, 691) * new Vector2(width_ratio, height_ratio), 
                    Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            }
            else
            {
                spriteBatch.DrawString(spriteFont, life, new Vector2(82, 690) * new Vector2(width_ratio, height_ratio),
                    Color.Black, 0.0f, Vector2.Zero, width_ratio, SpriteEffects.None, 0);
                spriteBatch.DrawString(spriteFont, life, new Vector2(81, 691) * new Vector2(width_ratio, height_ratio), 
                    Color.White, 0.0f, Vector2.Zero, width_ratio, SpriteEffects.None, 0);
            }
#if WINDOWS
            //we change text size accordingly to screen resolution
            if (GraphicsDevice.Viewport.Width > 1366)
            {
                spriteBatch.DrawString(spriteFont, "Press \"ESC\" to pause the game", 
                    new Vector2(352, 720) * new Vector2(width_ratio, height_ratio), 
                        Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
                spriteBatch.DrawString(spriteFont, "Press \"ESC\" to pause the game", 
                    new Vector2(351, 721) * new Vector2(width_ratio, height_ratio), 
                        Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            }
            else
            {
                spriteBatch.DrawString(spriteFont, "Press \"ESC\" to pause the game", 
                    new Vector2(352, 720) * new Vector2(width_ratio, height_ratio),
                        Color.Black, 0.0f, Vector2.Zero, width_ratio, SpriteEffects.None, 0);
                spriteBatch.DrawString(spriteFont, "Press \"ESC\" to pause the game", 
                    new Vector2(351, 721) * new Vector2(width_ratio, height_ratio),
                        Color.White, 0.0f, Vector2.Zero, width_ratio, SpriteEffects.None, 0);
            }

#elif XBOX
            //we change text size accordingly to screen resolution
            if (GraphicsDevice.Viewport.Width > 1366)
            {
                spriteBatch.DrawString(spriteFont, "Press \"Y\" to pause the game", 
                    new Vector2(352, 720) * new Vector2(width_ratio, height_ratio), 
                        Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
                spriteBatch.DrawString(spriteFont, "Press \"Y\" to pause the game", 
                    new Vector2(351, 721) * new Vector2(width_ratio, height_ratio), 
                        Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            }
            else
            {
                spriteBatch.DrawString(spriteFont, "Press \"Y\" to pause the game", 
                    new Vector2(352, 720) * new Vector2(width_ratio, height_ratio), 
                        Color.Black, 0.0f, Vector2.Zero, width_ratio, SpriteEffects.None, 0);
                spriteBatch.DrawString(spriteFont, "Press \"Y\" to pause the game", 
                    new Vector2(351, 721) * new Vector2(width_ratio, height_ratio), 
                        Color.White, 0.0f, Vector2.Zero, width_ratio, SpriteEffects.None, 0);
            }
#endif

            //here we draw the time bar
            spriteBatch.Draw(empty, new Rectangle((int)(1018 * width_ratio), (int)(703 * height_ratio),
                (int)((WinLogic.time_limit + 1) * 4 * width_ratio), (int)(24 * height_ratio)),
                    new Rectangle((int)(800 * width_ratio), (int)(700 * height_ratio), 
                        (int)(WinLogic.time_limit * 4 * width_ratio), (int)(20 * height_ratio)), 
                            Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0);

            spriteBatch.Draw(empty, new Rectangle((int)(1020 * width_ratio), (int)(705 * height_ratio),
                (int)(WinLogic.time_limit * 4 * width_ratio), (int)(20 * height_ratio)),
                    new Rectangle((int)(800 * width_ratio),  (int)(700 * height_ratio), 
                        (int)(WinLogic.time_limit * 4 * width_ratio), (int)(20 * height_ratio)),
                            Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0);


            if (GameState.game_state.time_counter > WinLogic.time_limit - WinLogic.time_limit / 4)
            {
                spriteBatch.Draw(empty, new Rectangle((int)(1020 * width_ratio), (int)(705 * height_ratio),
                    (int)(WinLogic.time_limit * 4 * width_ratio) - (int)(GameState.game_state.time_counter * 
                        4 * width_ratio), (int)(20 * height_ratio)), new Rectangle((int)(800 * width_ratio), 
                            (int)(700 * height_ratio),(int)(WinLogic.time_limit * 4 * width_ratio) - 
                                (int)(GameState.game_state.time_counter * 4 * width_ratio), (int)(20 * height_ratio)),
                                    Color.Red, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
            }
            else
            {
                spriteBatch.Draw(empty, new Rectangle((int)(1020 * width_ratio), (int)(705 * height_ratio),
                    (int)(WinLogic.time_limit * 4 * width_ratio) - (int)(GameState.game_state.time_counter * 
                        4 * width_ratio), (int)(20 * height_ratio)), new Rectangle((int)(800 * width_ratio),
                            (int)(700 * height_ratio), (int)(WinLogic.time_limit * 4 * width_ratio) - 
                                (int)(GameState.game_state.time_counter * 4 * width_ratio), (int)(20 * height_ratio)),
                                    Color.Green, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
            }

            if (GraphicsDevice.Viewport.Width > 1366)
            {
                spriteBatch.DrawString(spriteFont, "Time:", 
                    new Vector2(900, 690) * new Vector2(width_ratio, height_ratio), 
                        Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
                spriteBatch.DrawString(spriteFont, "Time:", 
                    new Vector2(901, 691) * new Vector2(width_ratio, height_ratio), 
                        Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            }
            else
            {
                spriteBatch.DrawString(spriteFont, "Time:", 
                    new Vector2(900, 690) * new Vector2(width_ratio, height_ratio), 
                        Color.Black, 0.0f, Vector2.Zero, width_ratio, SpriteEffects.None, 0);
                spriteBatch.DrawString(spriteFont, "Time:", 
                    new Vector2(901, 691) * new Vector2(width_ratio, height_ratio), 
                        Color.White, 0.0f, Vector2.Zero, width_ratio, SpriteEffects.None, 0);
            }

            if (GraphicsDevice.Viewport.Width > 1366)
            {
                spriteBatch.DrawString(spriteFont, "Level: " + GameState.game_state.level, 
                    new Vector2(1100, 13) * new Vector2(width_ratio, height_ratio), 
                        Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
                spriteBatch.DrawString(spriteFont, "Level: " + GameState.game_state.level, 
                    new Vector2(1101, 14) * new Vector2(width_ratio, height_ratio), 
                        Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            }
            else
            {
                spriteBatch.DrawString(spriteFont, "Level: " + GameState.game_state.level, 
                    new Vector2(1100, 13) * new Vector2(width_ratio, height_ratio),     
                        Color.Black, 0.0f, Vector2.Zero, width_ratio, SpriteEffects.None, 0);
                spriteBatch.DrawString(spriteFont, "Level: " + GameState.game_state.level, 
                    new Vector2(1101, 14) * new Vector2(width_ratio, height_ratio), 
                        Color.White, 0.0f, Vector2.Zero, width_ratio, SpriteEffects.None, 0);
            }

            //end of text drawing (GUI)

            //here we draw splash screen text
            if (t.timer < GameTimer.TIME_LIMIT  && WinLogic.win_level == false && WinLogic.lost_level == false)
            {
                scale += dt;
                if (scale >= 2.0f) scale = 1.0f;
                string ready = "GET READY";
                string time = "" + (int)(GameTimer.TIME_LIMIT - t.timer);
                spriteBatch.DrawString(startFont, ready, 
                    new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2 - 150), 
                        Color.Green, 0.0f, new Vector2(startFont.MeasureString(ready).X / 2, 0), width_ratio, 
                            SpriteEffects.None, 0);
                spriteBatch.DrawString(startFont, time, 
                    new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2), 
                        Color.Green, 0.0f, new Vector2(startFont.MeasureString(time).X / 2, 0), width_ratio * scale, 
                            SpriteEffects.None, 0);
            }
      

            if (WinLogic.win_level == true && timer < 3)
            {
                Game1.isWaiting = true;
                spriteBatch.DrawString(startFont, "LEVEL COMPLETED!", 
                    new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2 - 150), 
                        Color.Green, 0.0f, new Vector2(startFont.MeasureString("LEVEL COMPLETED!").X / 2, 0), 
                            width_ratio, SpriteEffects.None, 0);
                timer += dt;
            }
            else if (WinLogic.win_level == true && timer > 3)
            {
                Game1.isWaiting = false;
                WinLogic.win_level = false;
                timer = 0.0f;
            }

            if (WinLogic.lost_level == true && timer < 3)
            {
                Game1.isWaiting = true;
                spriteBatch.DrawString(splashFont, "GAME OVER", 
                    new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2 - 150), 
                        Color.DarkRed, 0.0f, new Vector2(splashFont.MeasureString("GAME OVER").X / 2, 0),
                            width_ratio, SpriteEffects.None, 0);
                timer += dt;
            }
            else if (WinLogic.lost_level == true && timer > 3)
            {
                Game1.isWaiting = false;
                WinLogic.lost_level = false;
                timer = 0.0f;
            }


            spriteBatch.End();

            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
        }


        #endregion
    }
}
        #endregion